package com.ag.common;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.ag.lzm.dao.TbUriDAO;

/**
 * 提供文件下载的工具Action
 * 
 * 
 * 调用时直接定向到链接即可： ag/file/download.action?fileRealName=
 * '我的附件.doc'&fileDiskURL='123.doc'&moduleID='105'
 * 
 * 即：
 * 
 * 
 * 解决中文乱码的方式是tomcat和工程文件的编码要一致，如都是UTF-8
 * 
 * 在tomact的conf文件夹下server.xml里作如下修改
 * 
 *  <Connector port="8888" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" URIEncoding="UTF-8" />
 * 
 * 
 * <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"  URIEncoding="UTF-8" />
 * 
 * 右击工程目录-properties-text file encoding 改为 UTF-8
 * 
 */
public class CopyOfDownloadAction extends BaseAction {
	private static final long serialVersionUID = -5672337753960737218L;
	@Resource(name = "LZMTbUriDAO")
	private TbUriDAO uriDAO;
	/**
	 * 附件真实名
	 */
	private String result;
	
	private String fileRealName;

	private String fileName;// 文件名
	
	private String downloadName;

	public String getDownloadName() {
		//System.out.println("fileName"+fileName);
		System.out.println("result"+result);
		return result;
		//return new String(result.getBytes(), "ISO-8859-1");
	}

	public void setDownloadName(String downloadName) {
		this.downloadName = downloadName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
		//this.fileName=new String(fileRealName.getBytes("iso-8859-1"), "UTF-8"); 
	}

	public void setFileRealName(String fileRealName) throws UnsupportedEncodingException {
		//this.fileRealName = fileRealName;
		this.fileRealName=new String(fileRealName.getBytes("iso-8859-1"), "UTF-8"); 
	}

	/**
	 * 附件服务器硬盘完整路径，文件名+文件夹名
	 */
	private String fileDiskURL;

	public void setFileDiskURL(String fileDiskURL) {
		this.fileDiskURL = fileDiskURL;
	}

	/**
	 * 模块ID
	 */
	private String moduleID;

	public void setModuleID(String moduleID) {
		this.moduleID = moduleID;
	}

	/**
	 * 下载的核心方法，方法名与配置文件里 <param
	 * name="inputName">inputStream</param>的"inputName"值一致，默认调用其Get方法
	 * 
	 * @return 输入流
	 * @throws UnsupportedEncodingException
	 * @throws FileNotFoundException
	 */
	public InputStream getInputStream() throws UnsupportedEncodingException,
			FileNotFoundException {
		
		String topPath = uriDAO.findById(moduleID).getUPath();
		//fileName= new String(fileName.getBytes(), "ISO-8859-1");
		//System.out.println("====hmk test:"+topPath+fileDiskURL);
		return new FileInputStream(topPath + fileDiskURL);

	}

	public String execute() throws Exception {
		return "success";
	}

	/**
	 * 工具方法 主要用于解析get方式提交请求时的请求参数，已经实现了编码的转换
	 * 
	 * @param 请求参数名
	 * @return 请求参数值
	 */
	public String getParameter(String param) {
		try {
			return new String(ServletActionContext.getRequest().getParameter(
					param).getBytes("ISO-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取文件名 方法名与配置文件里 <param name="contentDisposition">
	 * attachment;filename="${fileName}" </param>的"fileName"值一致，默认调用其Get方法 *
	 * 
	 * @return 文件名
	 * @throws UnsupportedEncodingException
	 */
	public String getFileName() throws UnsupportedEncodingException {
		return fileName = new String(fileRealName.getBytes(),"iso8859-1");  
		//return fileName = new String(fileRealName.getBytes(),"utf-8"); 
		// return new String(getParameter(fileRealName).getBytes(),
		// "ISO-8859-1");

		// System.out.println(fileRealName+"fileRealName");
		// String tm = new String(fileRealName.getBytes(), "ISO8859-1");
		// return tm;

		//System.out.println(fileRealName);
		//System.out.println(getParameter(fileRealName)+"fileRaleName");
		//ServletActionContext.getResponse().setHeader("charset", "ISO8859-1");
		//String tmp= new String(fileRealName.getBytes(), "ISO8859-1");
	    // return java.net.URLEncoder.encode(tmp, "ISO8859-1"); //这句很重要，不然文件名为乱码
		
		
//		result = 
//			 new String(fileRealName.getBytes("iso-8859-1"), "UTF-8"); 
//		System.out.println("result0"+result);
//		//return result;
//		return new String(result.getBytes(), "ISO-8859-1");
//		//return result;
		
		//return new String(fileRealName.getBytes("iso-8859-1"), "UTF-8");
		//return new String(this.fileRealName.getBytes(), "ISO8859-1");
		
		
		
		//return new String(fileName.getBytes(), "ISO8859-1");

		// System.out.println(fileRealName);
		// String aaString= new String(fileRealName.getBytes("ISO-8859-1"),
		// "utf-8");
		// System.out.println(getParameter(fileRealName));
		// System.out.println(fileRealName.getBytes("ISO-8859-1"));
		// System.out.println(aaString);
		// return aaString;

		// return getParameter(fileRealName);
		// return new String("黎肇明.txt".getBytes(),"ISO8859-1");
	}

}
